package com.home.fitzx.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.dexcoder.commons.pager.Pager;
import com.home.fitzx.dto.QueryUserParams;
import com.home.fitzx.entity.User;
import com.home.fitzx.service.UserService;
import com.home.fitzx.utils.FitzUtils;


@Controller
@RequestMapping("/web/user")
public class UserController {

    @Autowired
    private UserService userService;
    
    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
    
    @RequestMapping(method = RequestMethod.GET)
    public String index(Model model){
		return "/user/index";
    }
    
    @RequestMapping(value="list", method = RequestMethod.GET)
    public String list(Model model){
    	return "/user/list";
    } 
    
    @RequestMapping("loadData")
    @ResponseBody
    public JSONObject loadData(QueryUserParams params, Model model){
    	JSONObject json = new JSONObject();
    	Pager pager = userService.findUsers(params);
    	json.put("aaData", pager.getList());
    	json.put("iTotalRecords", pager.getItemsTotal());
    	json.put("iTotalDisplayRecords", pager.getItemsTotal());
    	return json;
    } 
    
    @RequestMapping(value="/add", method = RequestMethod.GET)
    public String add(Model model){
        return "/user/add";
    } 
    
    @RequestMapping(value="/create", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject create(User user, Model model){
    	JSONObject result = new JSONObject();
    	try {
    		userService.save(user);
    		result.put("code", 0);
    	} catch(DuplicateKeyException dkException) {
    		result.put("code", 1);
    		result.put("message", FitzUtils.format("用户名:{} 重复", user.getName()));
    	} catch(Exception e) {
    		e.printStackTrace();
    		LOG.error(e.getMessage());
    		result.put("code", 1);
    		result.put("message", e.getMessage());
    	}
        return result;
    } 
    
    @RequestMapping(value="/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable Long id, Model model){
        model.addAttribute("user", userService.getById(id));
        return "/user/edit";
    } 
    
    @RequestMapping(value="/update", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject update(User user, Model model){
    	JSONObject result = new JSONObject();
    	try {
    		userService.update(user);
    		result.put("code", 0);
    	} catch(DuplicateKeyException dkException) {
    		result.put("code", 1);
    		result.put("message", FitzUtils.format("用户名:{} 重复", user.getName()));
    	} catch(Exception e) {
    		e.printStackTrace();
    		LOG.error(e.getMessage());
    		result.put("code", 1);
    		result.put("message", e.getMessage());
    	}
        return result;
    } 
    
    @RequestMapping(value="/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject delete(@PathVariable Integer id, Model model){
    	JSONObject result = new JSONObject();
    	try {
    		userService.delete(id);
    		result.put("code", 0);
    	} catch(Exception e) {
    		e.printStackTrace();
    		LOG.error(e.getMessage());
    		result.put("code", 1);
    		result.put("message", e.getMessage());
    	}
        return result;
    } 
}
